#!/bin/bash

# 全局镜像加速一键配置与验证脚本
#
# 说明:
# - 本脚本可一键为宿主机配置常见包管理器与 Docker 的镜像加速；
# - 同时内置验证步骤，检查镜像源是否可访问/可用；
# - 自动备份被修改的原配置文件（*.backup）。
#
# 支持的项目（检测到工具才配置）:
# - APT (Ubuntu/Debian): 清华源
# - pip (系统级/用户级): 清华 PyPI
# - Docker daemon: 多个 registry mirrors
# - npm / yarn / pnpm: npmmirror
# - Go: GOPROXY=goproxy.cn
# - Cargo: rsproxy.cn
# - RubyGems: ruby-china 镜像
# - Maven: 阿里云公共库镜像
# - Conda: 清华镜像
#
# 用法:
#   bash setup_global_mirrors.sh                 # 默认: 先配置再验证
#   bash setup_global_mirrors.sh --config-only   # 仅配置, 不执行验证
#   bash setup_global_mirrors.sh --verify-only   # 仅验证, 不做配置
#   bash setup_global_mirrors.sh --help          # 查看帮助

set -e

SHOW_HELP=false
VERIFY_ONLY=false
CONFIG_ONLY=false

for arg in "$@"; do
  case "$arg" in
    --help|-h)
      SHOW_HELP=true
      ;;
    --verify-only)
      VERIFY_ONLY=true
      ;;
    --config-only)
      CONFIG_ONLY=true
      ;;
    *)
      echo "未知参数: $arg" >&2
      SHOW_HELP=true
      ;;
  esac
done

if [ "$SHOW_HELP" = true ]; then
  cat <<'USAGE'
用法:
  bash setup_global_mirrors.sh                 # 默认: 先配置再验证
  bash setup_global_mirrors.sh --config-only   # 仅配置, 不执行验证
  bash setup_global_mirrors.sh --verify-only   # 仅验证, 不做配置
  bash setup_global_mirrors.sh --help          # 查看帮助

说明:
  - 自动检测工具存在与否, 有则配置; 无则跳过并提示。
  - 修改的系统级文件会先备份为同名 .backup。
USAGE
  exit 0
fi

# 全局镜像加速配置脚本
# 此脚本将配置宿主机全局镜像加速

# 内部工具: 简易可用性测试 (HEAD 请求)
test_url() {
  local name=$1
  local url=$2
  local timeout=${3:-10}
  echo -n "测试 $name: "
  if timeout "$timeout" curl -sI "$url" 2>&1 | head -1 | grep -qE "(200|301|302|HTTP)"; then
    echo -e "${GREEN}✓ 可用${NC}"
    return 0
  else
    echo -e "${YELLOW}⚠ 不可用${NC}"
    return 1
  fi
}

echo "=========================================="
echo "配置与验证 全局镜像加速"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then 
    echo -e "${YELLOW}注意: 某些配置需要root权限，将以sudo执行${NC}"
    SUDO="sudo"
else
    SUDO=""
fi

echo "=========================================="
echo "1. 配置系统级 pip 镜像源"
echo "=========================================="

PIP_MIRROR="https://pypi.tuna.tsinghua.edu.cn/simple"
PIP_TRUSTED_HOST="pypi.tuna.tsinghua.edu.cn"

# 配置系统级pip（需要root权限）
if [ -f /etc/pip.conf ]; then
    echo "系统级pip配置已存在，备份为 /etc/pip.conf.backup"
    $SUDO cp /etc/pip.conf /etc/pip.conf.backup
fi

echo "配置系统级pip使用清华镜像源..."
$SUDO mkdir -p /etc
$SUDO tee /etc/pip.conf > /dev/null <<EOF
[global]
index-url = ${PIP_MIRROR}
trusted-host = ${PIP_TRUSTED_HOST}
EOF

if [ -f /etc/pip.conf ]; then
    echo -e "${GREEN}✓ 系统级pip配置已设置: /etc/pip.conf${NC}"
    cat /etc/pip.conf
else
    echo -e "${RED}✗ 系统级pip配置失败${NC}"
    exit 1
fi

echo ""

echo "=========================================="
echo "2. 配置用户级 pip 镜像源"
echo "=========================================="

# 配置用户级pip
if [ -f ~/.pip/pip.conf ]; then
    echo "用户级pip配置已存在，备份为 ~/.pip/pip.conf.backup"
    cp ~/.pip/pip.conf ~/.pip/pip.conf.backup
fi

echo "配置用户级pip使用清华镜像源（与Dockerfile一致）..."
mkdir -p ~/.pip
tee ~/.pip/pip.conf > /dev/null <<EOF
[global]
index-url = ${PIP_MIRROR}
trusted-host = ${PIP_TRUSTED_HOST}
EOF

if [ -f ~/.pip/pip.conf ]; then
    echo -e "${GREEN}✓ 用户级pip配置已设置: ~/.pip/pip.conf${NC}"
    cat ~/.pip/pip.conf
else
    echo -e "${RED}✗ 用户级pip配置失败${NC}"
    exit 1
fi

echo ""

echo "=========================================="
echo "3. 配置 APT 镜像源 (Ubuntu/Debian)"
echo "=========================================="

# 标记APT是否已配置
APT_CONFIGURED=false

if [ -f /etc/os-release ]; then
  . /etc/os-release
  OS_ID="${ID}"
  OS_CODENAME="${VERSION_CODENAME}"

  # 兼容未提供 VERSION_CODENAME 的情况
  if [ -z "$OS_CODENAME" ] && [ -r /etc/os-release ]; then
    OS_CODENAME=$(grep -E '^VERSION_CODENAME=' /etc/os-release | cut -d'=' -f2)
  fi

  case "$OS_ID" in
    ubuntu)
      echo "检测到 Ubuntu，准备配置 APT 清华镜像..."
      if [ -z "$OS_CODENAME" ] && command -v lsb_release >/dev/null 2>&1; then
        OS_CODENAME=$(lsb_release -sc)
      fi
      if [ -z "$OS_CODENAME" ]; then
        echo -e "${YELLOW}⚠ 无法识别 Ubuntu 版本代号，跳过APT配置${NC}"
      else
        if [ -f /etc/apt/sources.list ]; then
          echo "备份 /etc/apt/sources.list 为 /etc/apt/sources.list.backup"
          $SUDO cp /etc/apt/sources.list /etc/apt/sources.list.backup
        fi
        echo "写入清华镜像到 /etc/apt/sources.list ..."
        $SUDO tee /etc/apt/sources.list > /dev/null <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME} main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME}-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME}-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME}-security main restricted universe multiverse
# 源码仓可按需开启
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME} main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME}-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME}-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${OS_CODENAME}-security main restricted universe multiverse
EOF
        echo "更新 APT 索引..."
        if $SUDO apt-get update; then
          APT_CONFIGURED=true
          echo -e "${GREEN}✓ APT 镜像已配置并更新索引${NC}"
        else
          echo -e "${YELLOW}⚠ APT 索引更新失败，但镜像源已写入${NC}"
        fi
      fi
      ;;
    debian)
      echo "检测到 Debian，准备配置 APT 清华镜像..."
      if [ -f /etc/apt/sources.list ]; then
        echo "备份 /etc/apt/sources.list 为 /etc/apt/sources.list.backup"
        $SUDO cp /etc/apt/sources.list /etc/apt/sources.list.backup
      fi
      echo "写入清华镜像到 /etc/apt/sources.list ..."
      $SUDO tee /etc/apt/sources.list > /dev/null <<'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ stable main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ stable-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ stable-backports main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security stable-security main contrib non-free non-free-firmware
# 源码仓可按需开启
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ stable main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ stable-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ stable-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security stable-security main contrib non-free non-free-firmware
EOF
      echo "更新 APT 索引..."
      if $SUDO apt-get update; then
        APT_CONFIGURED=true
        echo -e "${GREEN}✓ APT 镜像已配置并更新索引${NC}"
      else
        echo -e "${YELLOW}⚠ APT 索引更新失败，但镜像源已写入${NC}"
      fi
      ;;
    *)
      echo -e "${YELLOW}⚠ 非 Ubuntu/Debian 系统（检测到: ${OS_ID:-unknown}），跳过APT配置${NC}"
      ;;
  esac
else
  echo -e "${YELLOW}⚠ 未找到 /etc/os-release，无法识别系统，跳过APT配置${NC}"
fi

echo ""

echo "=========================================="
echo "4. 配置 Docker 镜像加速配置"
echo "=========================================="

DOCKER_DAEMON_JSON="/etc/docker/daemon.json"

# 使用验证报告中的镜像源
DOCKER_MIRRORS=(
  "https://87avm5ie.mirror.aliyuncs.com"
  "https://docker.1ms.run"
  "https://docker.xuanyuan.me"
  "https://docker.mirrors.ustc.edu.cn"
  "https://hub-mirror.c.163.com"
)

$SUDO mkdir -p /etc/docker

if [ -f "$DOCKER_DAEMON_JSON" ]; then
    echo "发现现有 Docker 配置，备份为 ${DOCKER_DAEMON_JSON}.backup"
    $SUDO cp "$DOCKER_DAEMON_JSON" "${DOCKER_DAEMON_JSON}.backup"
fi

echo "写入 Docker 镜像加速器配置..."
$SUDO tee "$DOCKER_DAEMON_JSON" > /dev/null <<EOF
{
  "registry-mirrors": [
    "${DOCKER_MIRRORS[0]}",
    "${DOCKER_MIRRORS[1]}",
    "${DOCKER_MIRRORS[2]}",
    "${DOCKER_MIRRORS[3]}",
    "${DOCKER_MIRRORS[4]}"
  ]
}
EOF

echo -e "${GREEN}✓ Docker 镜像加速器已配置到: ${DOCKER_DAEMON_JSON}${NC}"
echo "当前配置:"
cat "$DOCKER_DAEMON_JSON"

# 尝试重启 Docker 使其生效
echo "尝试重启 Docker 服务以应用配置..."
if command -v systemctl >/dev/null 2>&1; then
  if $SUDO systemctl is-active --quiet docker; then
    $SUDO systemctl restart docker && echo -e "${GREEN}✓ Docker 已重启${NC}" || echo -e "${YELLOW}⚠ 尝试重启 Docker 失败，请手动重启${NC}"
  else
    echo -e "${YELLOW}⚠ Docker 服务未运行，跳过重启${NC}"
  fi
else
  echo -e "${YELLOW}⚠ 未检测到 systemctl，若需要请手动重启 Docker${NC}"
fi

echo ""

echo "=========================================="
echo "5. 配置 Node.js 包管理器镜像 (npm/yarn/pnpm)"
echo "=========================================="

NPM_CONFIGURED=false
if command -v npm >/dev/null 2>&1; then
  echo "检测到 npm，配置 ~/.npmrc..."
  if [ -f ~/.npmrc ]; then
    echo "备份 ~/.npmrc 为 ~/.npmrc.backup"
    cp ~/.npmrc ~/.npmrc.backup || true
  fi
  tee ~/.npmrc > /dev/null <<EOF
registry=https://registry.npmmirror.com
strict-ssl=false
fetch-retry-maxtimeout=120000
fetch-retry-mintimeout=20000
fetch-retries=3
EOF
  echo -e "${GREEN}✓ npm 已配置为 npmmirror${NC}"
  NPM_CONFIGURED=true
else
  echo -e "${YELLOW}⚠ 未检测到 npm，跳过npm配置${NC}"
fi

if command -v yarn >/dev/null 2>&1; then
  echo "检测到 yarn，设置 registry..."
  yarn config set registry https://registry.npmmirror.com >/dev/null 2>&1 || true
  echo -e "${GREEN}✓ yarn 已配置为 npmmirror${NC}"
fi

if command -v pnpm >/dev/null 2>&1; then
  echo "检测到 pnpm，设置 registry..."
  pnpm config set registry https://registry.npmmirror.com >/dev/null 2>&1 || true
  echo -e "${GREEN}✓ pnpm 已配置为 npmmirror${NC}"
fi

echo ""

echo "=========================================="
echo "6. 配置 Go 模块代理 (GOPROXY)"
echo "=========================================="

GO_CONFIGURED=false
if command -v go >/dev/null 2>&1; then
  echo "检测到 Go，写入 GOPROXY 配置..."
  # 用户会话
  if ! grep -q "GOPROXY=" ~/.profile 2>/dev/null; then
    echo 'export GOPROXY=https://goproxy.cn,direct' >> ~/.profile
  fi
  # 系统级（需要root）
  $SUDO mkdir -p /etc/profile.d
  $SUDO bash -c 'cat > /etc/profile.d/goproxy.sh <<EOF
export GOPROXY=https://goproxy.cn,direct
EOF'
  echo -e "${GREEN}✓ GOPROXY 已配置为 goproxy.cn${NC}"
  GO_CONFIGURED=true
else
  echo -e "${YELLOW}⚠ 未检测到 go，跳过GOPROXY配置${NC}"
fi

echo ""

echo "=========================================="
echo "7. 配置 Cargo (Rust) 镜像源"
echo "=========================================="

CARGO_CONFIGURED=false
if command -v cargo >/dev/null 2>&1; then
  echo "检测到 cargo，写入 ~/.cargo/config.toml ..."
  mkdir -p ~/.cargo
  if [ -f ~/.cargo/config.toml ]; then
    echo "备份 ~/.cargo/config.toml 为 ~/.cargo/config.toml.backup"
    cp ~/.cargo/config.toml ~/.cargo/config.toml.backup || true
  fi
  tee ~/.cargo/config.toml > /dev/null <<'EOF'
[source.crates-io]
replace-with = 'rsproxy'

[source.rsproxy]
registry = 'https://rsproxy.cn/crates.io-index'

[registries]
default = 'rsproxy'

[net]
git-fetch-with-cli = true
EOF
  echo -e "${GREEN}✓ Cargo 已配置为 rsproxy.cn${NC}"
  CARGO_CONFIGURED=true
else
  echo -e "${YELLOW}⚠ 未检测到 cargo，跳过Cargo配置${NC}"
fi

echo ""

echo "=========================================="
echo "8. 配置 RubyGems 镜像源"
echo "=========================================="

GEM_CONFIGURED=false
if command -v gem >/dev/null 2>&1; then
  echo "检测到 gem，写入 ~/.gemrc ..."
  if [ -f ~/.gemrc ]; then
    echo "备份 ~/.gemrc 为 ~/.gemrc.backup"
    cp ~/.gemrc ~/.gemrc.backup || true
  fi
  tee ~/.gemrc > /dev/null <<'EOF'
sources:
  - https://gems.ruby-china.com/
backtrace: false
verbose: true
EOF
  echo -e "${GREEN}✓ RubyGems 已配置为 ruby-china 镜像${NC}"
  GEM_CONFIGURED=true
else
  echo -e "${YELLOW}⚠ 未检测到 gem，跳过RubyGems配置${NC}"
fi

echo ""

echo "=========================================="
echo "9. 配置 Maven 镜像源 (~/.m2/settings.xml)"
echo "=========================================="

MAVEN_CONFIGURED=false
if command -v mvn >/dev/null 2>&1; then
  echo "检测到 mvn，写入 ~/.m2/settings.xml ..."
  mkdir -p ~/.m2
  if [ -f ~/.m2/settings.xml ]; then
    echo "备份 ~/.m2/settings.xml 为 ~/.m2/settings.xml.backup"
    cp ~/.m2/settings.xml ~/.m2/settings.xml.backup || true
  fi
  tee ~/.m2/settings.xml > /dev/null <<'EOF'
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <mirrors>
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>*</mirrorOf>
      <name>Aliyun Maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>
</settings>
EOF
  echo -e "${GREEN}✓ Maven 已配置为阿里云镜像${NC}"
  MAVEN_CONFIGURED=true
else
  echo -e "${YELLOW}⚠ 未检测到 mvn，跳过Maven配置${NC}"
fi

echo ""

echo "=========================================="
echo "10. 配置 Conda 镜像 (~/.condarc)"
echo "=========================================="

CONDA_CONFIGURED=false
if command -v conda >/dev/null 2>&1; then
  echo "检测到 conda，写入 ~/.condarc ..."
  if [ -f ~/.condarc ]; then
    echo "备份 ~/.condarc 为 ~/.condarc.backup"
    cp ~/.condarc ~/.condarc.backup || true
  fi
  tee ~/.condarc > /dev/null <<'EOF'
channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
EOF
  echo -e "${GREEN}✓ Conda 已配置为清华镜像${NC}"
  CONDA_CONFIGURED=true
else
  echo -e "${YELLOW}⚠ 未检测到 conda，跳过Conda配置${NC}"
fi

echo ""

verify_all() {
  echo "=========================================="
  echo "验证配置有效性"
  echo "=========================================="

  # Docker daemon registry mirrors
  echo "- 验证 Docker 镜像加速 (daemon.json)"
  if [ -f /etc/docker/daemon.json ]; then
    if grep -q "registry-mirrors" /etc/docker/daemon.json; then
      echo -e "${GREEN}✓ Docker daemon 已存在 registry-mirrors${NC}"
    else
      echo -e "${YELLOW}⚠ /etc/docker/daemon.json 未发现 registry-mirrors${NC}"
    fi
  else
    echo -e "${YELLOW}⚠ /etc/docker/daemon.json 不存在${NC}"
  fi

  # APT 核心镜像连通性 (如为 Debian/Ubuntu)
  if [ -f /etc/os-release ]; then
    . /etc/os-release
    if [ "${ID}" = "debian" ]; then
      test_url "清华 Debian 镜像" "https://mirrors.tuna.tsinghua.edu.cn/debian/dists/stable/InRelease" 10 || true
      test_url "清华 Debian 安全更新" "https://mirrors.tuna.tsinghua.edu.cn/debian-security/dists/stable-security/InRelease" 10 || true
    elif [ "${ID}" = "ubuntu" ]; then
      codename=${VERSION_CODENAME:-focal}
      test_url "清华 Ubuntu 镜像" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${codename}/InRelease" 10 || true
    fi
  fi

  # pip 镜像连通性
  test_url "清华 PyPI" "https://pypi.tuna.tsinghua.edu.cn/simple/" 10 || true

  # Node 包管理器
  if command -v npm >/dev/null 2>&1; then
    test_url "npmmirror" "https://registry.npmmirror.com/" 10 || true
  fi

  # Go
  if command -v go >/dev/null 2>&1; then
    echo -n "GOPROXY: "; echo "${GOPROXY:-$(grep -E '^export GOPROXY=' ~/.profile 2>/dev/null | cut -d'=' -f2)}"
  fi

  # Cargo
  if command -v cargo >/dev/null 2>&1; then
    if [ -f ~/.cargo/config.toml ] && grep -q "rsproxy.cn" ~/.cargo/config.toml; then
      echo -e "${GREEN}✓ Cargo 已配置 rsproxy.cn${NC}"
    else
      echo -e "${YELLOW}⚠ Cargo 未检测到 rsproxy.cn${NC}"
    fi
  fi

  # RubyGems
  if command -v gem >/dev/null 2>&1; then
    if [ -f ~/.gemrc ] && grep -q "gems.ruby-china.com" ~/.gemrc; then
      echo -e "${GREEN}✓ RubyGems 已配置 ruby-china 镜像${NC}"
    else
      echo -e "${YELLOW}⚠ RubyGems 未检测到 ruby-china 镜像${NC}"
    fi
  fi

  # Maven
  if command -v mvn >/dev/null 2>&1; then
    if [ -f ~/.m2/settings.xml ] && grep -q "maven.aliyun.com" ~/.m2/settings.xml; then
      echo -e "${GREEN}✓ Maven 已配置阿里云镜像${NC}"
    else
      echo -e "${YELLOW}⚠ Maven 未检测到阿里云镜像${NC}"
    fi
  fi

  # Conda
  if command -v conda >/dev/null 2>&1; then
    if [ -f ~/.condarc ] && grep -q "mirrors.tuna.tsinghua.edu.cn" ~/.condarc; then
      echo -e "${GREEN}✓ Conda 已配置清华镜像${NC}"
    else
      echo -e "${YELLOW}⚠ Conda 未检测到清华镜像${NC}"
    fi
  fi
}

# 验证pip配置
echo "验证pip配置..."
if command -v pip3 &> /dev/null; then
    echo "pip3 配置:"
    pip3 config list 2>/dev/null | grep -E "index-url|trusted-host" || echo "  无法读取配置"
elif command -v pip &> /dev/null; then
    echo "pip 配置:"
    pip config list 2>/dev/null | grep -E "index-url|trusted-host" || echo "  无法读取配置"
else
    echo -e "${YELLOW}⚠ pip命令未找到，跳过验证${NC}"
fi

echo ""

echo "=========================================="
echo "配置完成总结"
echo "=========================================="
if [ "$NPM_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ npm/yarn/pnpm: 已配置为 npmmirror${NC}"
else
  echo -e "${YELLOW}• npm/yarn/pnpm: 未更改或已跳过${NC}"
fi
if [ "$GO_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ GOPROXY: https://goproxy.cn,direct${NC}"
else
  echo -e "${YELLOW}• GOPROXY: 未更改或已跳过${NC}"
fi
if [ "$CARGO_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ Cargo: rsproxy.cn${NC}"
else
  echo -e "${YELLOW}• Cargo: 未更改或已跳过${NC}"
fi
if [ "$GEM_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ RubyGems: ruby-china 镜像${NC}"
else
  echo -e "${YELLOW}• RubyGems: 未更改或已跳过${NC}"
fi
if [ "$MAVEN_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ Maven: 阿里云镜像${NC}"
else
  echo -e "${YELLOW}• Maven: 未更改或已跳过${NC}"
fi
if [ "$CONDA_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ Conda: 清华镜像${NC}"
else
  echo -e "${YELLOW}• Conda: 未更改或已跳过${NC}"
fi
if [ "$APT_CONFIGURED" = true ]; then
  echo -e "${GREEN}✓ APT 镜像: 清华源 (已写入 /etc/apt/sources.list)${NC}"
else
  echo -e "${YELLOW}• APT 镜像: 未更改或已跳过${NC}"
fi
echo -e "${GREEN}✓ 系统级pip配置: /etc/pip.conf${NC}"
echo -e "${GREEN}✓ 用户级pip配置: ~/.pip/pip.conf${NC}"
echo -e "${GREEN}✓ 镜像源: ${PIP_MIRROR}${NC}"
echo ""

# 根据参数决定是否执行验证
if [ "$CONFIG_ONLY" = true ]; then
  echo "根据 --config-only 参数，跳过验证。"
elif [ "$VERIFY_ONLY" = true ]; then
  echo "根据 --verify-only 参数，仅执行验证。"
  verify_all
else
  echo "开始执行验证..."
  verify_all
fi
echo ""
echo "所有pip命令（pip、pip3、python -m pip等）现在都会使用清华镜像源"
echo "这与 Dockerfile 中的配置保持一致"
echo ""
echo "注意: 如果之前使用过pip，可能需要清除缓存："
echo "  pip cache purge  # 清除pip缓存"
echo ""

